package third.session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "SessionServlet", value = "/session")
public class SessionServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        System.out.println("sessionId:" + session.getId());
        System.out.println("getCreationTime:" + session.getCreationTime());
        System.out.println("getLastAccessedTime:" + session.getLastAccessedTime());
        System.out.println("getMaxInactiveInterval:" + session.getMaxInactiveInterval());
        System.out.println("session已经创建了...........");

        session.setAttribute("username", "张三");
        String username = (String) session.getAttribute("username");
        System.out.println(username);
        session.removeAttribute("username");
        //session失效
        session.invalidate();


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

}
